import subprocess
import sys
import pkg_resources

def install_package(package):
    """安装Python包"""
    subprocess.check_call([sys.executable, "-m", "pip", "install", package])

def check_package(package_name):
    """检查包是否已安装"""
    try:
        pkg_resources.get_distribution(package_name)
        return True
    except pkg_resources.DistributionNotFound:
        return False

def main():
    """主函数，安装所需依赖"""
    print("检查并安装手语识别摄像头客户端所需依赖...")
    
    # 需要的包列表
    packages = {
        "opencv-python": "用于摄像头访问和视频处理",
        "mediapipe": "用于骨骼关键点提取",
        "requests": "用于与服务器通信",
        "numpy": "用于数值计算"
    }
    
    # 检查并安装缺失的包
    for package, description in packages.items():
        if not check_package(package):
            print(f"正在安装 {package} ({description})...")
            try:
                install_package(package)
                print(f"{package} 安装成功！")
            except Exception as e:
                print(f"{package} 安装失败: {e}")
                print(f"请手动安装: pip install {package}")
        else:
            print(f"{package} 已安装")
    
    print("\n所有依赖检查完成！")
    print("您现在可以运行 'python camera_client.py' 启动摄像头手语识别客户端")

if __name__ == "__main__":
    main()
